package org.glandais.birt.osgi.oda.util;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * The Class SQLTypes.
 */
public class SQLTypes {

	/**
	 * Instantiates a new SQL types.
	 */
	private SQLTypes() {
		super();
	}

	/** The basic types. */
	private static Map<Class<?>, Integer> basicTypes = null;

	static {
		basicTypes = new HashMap<Class<?>, Integer>(30);
		basicTypes.put(Boolean.TYPE, new Integer(Types.BOOLEAN));
		basicTypes.put(Integer.TYPE, new Integer(Types.INTEGER));
		basicTypes.put(Long.TYPE, new Integer(Types.BIGINT));
		basicTypes.put(Byte.TYPE, new Integer(Types.TINYINT));
		basicTypes.put(Short.TYPE, new Integer(Types.SMALLINT));
		basicTypes.put(Float.TYPE, new Integer(Types.REAL));
		basicTypes.put(Double.TYPE, new Integer(Types.DOUBLE));
		basicTypes.put(Boolean.class, new Integer(Types.BOOLEAN));
		basicTypes.put(Integer.class, new Integer(Types.INTEGER));
		basicTypes.put(Long.class, new Integer(Types.BIGINT));
		basicTypes.put(Byte.class, new Integer(Types.TINYINT));
		basicTypes.put(Short.class, new Integer(Types.SMALLINT));
		basicTypes.put(Float.class, new Integer(Types.REAL));
		basicTypes.put(Double.class, new Integer(Types.DOUBLE));
		basicTypes.put(String.class, new Integer(Types.VARCHAR));
		basicTypes.put(java.math.BigDecimal.class, new Integer(Types.DECIMAL));
		basicTypes.put(byte[].class, new Integer(Types.VARBINARY));
		basicTypes.put(java.sql.Timestamp.class, new Integer(Types.TIMESTAMP));
		basicTypes.put(java.sql.Time.class, new Integer(Types.TIME));
		basicTypes.put(java.sql.Date.class, new Integer(Types.DATE));
		basicTypes.put(java.sql.Ref.class, new Integer(Types.REF));
		basicTypes.put(java.sql.Blob.class, new Integer(Types.BLOB));
		basicTypes.put(java.sql.Clob.class, new Integer(Types.CLOB));
		basicTypes.put(java.sql.Array.class, new Integer(Types.ARRAY));
		basicTypes.put(java.sql.Struct.class, new Integer(Types.STRUCT));
		basicTypes.put(java.util.Date.class, new Integer(Types.TIMESTAMP));
		basicTypes.put(java.util.Calendar.class, new Integer(Types.TIMESTAMP));
		basicTypes.put(java.util.GregorianCalendar.class, new Integer(
				Types.TIMESTAMP));
	}

	/**
	 * Gets the SQL type.
	 * 
	 * @param clazz
	 *            the clazz
	 * 
	 * @return the SQL type
	 */
	public static final int getSQLType(Class<?> clazz) {
		Class<?> clazzType = clazz;
		while (clazzType != null) {
			Integer type = basicTypes.get(clazzType);
			if (type != null) {
				return type.intValue();
			}
			clazzType = clazzType.getSuperclass();
		}
		return java.sql.Types.JAVA_OBJECT; // as defined in data set extension
	}
}
